package top.wuyang.springboot3.controller;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.wuyang.springboot3.annotation.BucketLimit;
import top.wuyang.springboot3.annotation.Limit;
import top.wuyang.springboot3.annotation.LimitMode;
import top.wuyang.springboot3.base.R;
import top.wuyang.springboot3.entity.EntityInfo;
import top.wuyang.springboot3.entity.auth.TbUser;
import top.wuyang.springboot3.entity.auth.TbUserInfo;
import top.wuyang.springboot3.service.auth.AuthService;
import top.wuyang.springboot3.validate.CommonValidate;

@Controller
@RequestMapping("/test")
@Slf4j
@RequiredArgsConstructor
public class TestController {

    private final AuthService authService;

    @PostMapping("/entity")
    @ResponseBody
    public R<EntityInfo> entity(@RequestBody EntityInfo entity) {
        log.debug("entity信息:{}", entity);
        return R.ok(entity);
    }

    @GetMapping("/limit")
    @ResponseBody
    @Limit(key = "/test/limit", timeout = 30)
    public R<String> limit(String info) {
        return R.ok(info);
    }


    @GetMapping("/iplimit")
    @ResponseBody
    @BucketLimit(key = "/test/iplimit", timeout = 5 * 30, mode = LimitMode.IP)
    public R<String> iplimit(String info) {
        return R.ok(info);
    }

    @GetMapping("/bucketlimit")
    @ResponseBody
    @BucketLimit(key = "/test/bucketlimit", timeout = 5 * 30, mode = LimitMode.GLOBAL)
    public R<String> bucketlimit(String info) {
        return R.ok(info);
    }

    @GetMapping("/auth1")
    @ResponseBody
    public R<TbUser> auth1() {
        return authService.info();
    }

    @GetMapping("/auth2")
    @ResponseBody
    public R<TbUser> auth2() {
        return authService.info();
    }

    @GetMapping("/auth3")
    @ResponseBody
    public R<TbUser> auth3() {
        return authService.info();
    }

    @PostMapping("/validate/add")
    @ResponseBody
    public R<TbUser> validateAdd(@RequestBody @Validated(CommonValidate.Add.class) TbUser tbUser) {
        return R.ok(tbUser);
    }

    @PostMapping("/validate/edit")
    @ResponseBody
    public R<TbUser> validateEdit(@RequestBody @Validated(CommonValidate.Edit.class) TbUser tbUser) {
        return R.ok(tbUser);
    }

    @PostMapping("/validate")
    @ResponseBody
    public R<TbUserInfo> validate(@RequestBody @Validated(CommonValidate.class) TbUserInfo tbUserInfo) {
        return R.ok(tbUserInfo);
    }


}
